﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Collections.Generic;
using System.IO.IsolatedStorage;

namespace PokerTimer
{
    public partial class MainPage : UserControl
    {
        int remainingMins = 0;
        int remainingSecs = 0;
        int defaultMins = 20;
        DispatcherTimer myDispatcherTimer;
        List<Blind> blindsList;

        ContextMenu cMenu = null;
        MenuItem menuItem;

        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();
            RoundDuration.Text = defaultMins.ToString();
            remainingMins = defaultMins;
        }

        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            myDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000);
            myDispatcherTimer.Tick += new EventHandler(Each_Tick);

            blindsList = new List<Blind> {
                new Blind { Small = 25, Big = 50 },
                new Blind { Small = 50, Big = 100 },
                new Blind { Small = 100, Big = 200 },
                new Blind { Small = 150, Big = 300 },
                new Blind { Small = 200, Big = 400 },
                new Blind { Small = 300, Big = 600 },
                new Blind { Small = 400, Big = 800 },
                new Blind { Small = 500, Big = 1000 },
                new Blind { Small = 750, Big = 1500 },
                new Blind { Small = 1000, Big = 2000 },
                new Blind { Small = 1500, Big = 3000 },
                new Blind { Small = 2000, Big = 4000 },
                new Blind { Small = 2500, Big = 5000 },
                new Blind { Small = 3000, Big = 6000 },
                new Blind { Small = 5000, Big = 10000 }
            };

            currentBlindsControl.LiveBlindsList = blindsList;
            currentBlindsControl.RefreshBlindValues();
            BlindsGrid.DataContext = blindsList;

        }

        void Ring()
        {
            remainingMins = Convert.ToInt16(RoundDuration.Text);
            DingMedia.Stop();
            DingMedia.Play();
            currentBlindsControl.IncrementBlinds();
        }

        // Fires every second
        public void Each_Tick(object o, EventArgs sender)
        {
            remainingSecs = remainingSecs - 1;

            if (remainingSecs < 0)
            {
                remainingMins = remainingMins - 1;
                remainingSecs = 59;
            }

            if (remainingMins < 0)
            {
                remainingMins = 0;
                remainingSecs = 0;
                Ring();
            }
            UpdateDisplay();
        }

        private void ResetButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            remainingMins = Convert.ToInt16(RoundDuration.Text);
            remainingSecs = 0;
            UpdateDisplay();
        }

        private void PlayButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (myDispatcherTimer.IsEnabled)
            {
                myDispatcherTimer.Stop();
                PlayButton.Content = "Play";
            }
            else
            {
                myDispatcherTimer.Start();
                PlayButton.Content = "Pause";
            }
        }

        private void FullScreenButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
            ResizeFonts();
        }

        void UpdateDisplay()
        {
            CountdownText.Text = String.Format("{0:00}", remainingMins) + ":" + String.Format("{0:00}", remainingSecs);
        }

        void Content_Resized(object sender, EventArgs e)
        {
            ResizeFonts();
        }

        void ResizeFonts()
        {
            //CountdownText.RenderTransform.
            
            if (Application.Current.Host.Content.ActualWidth > 0.0)
            {
                CountdownText.FontSize = Convert.ToInt16(Application.Current.Host.Content.ActualWidth / 2.5);
                var a = CountdownText.ActualWidth;
                double widt = (-(System.Convert.ToInt16(CountdownText.ActualWidth)) / 2);
                CountdownText.SetValue(Canvas.LeftProperty, Convert.ToDouble(-CountdownText.ActualWidth / 2));
            }
        }

        private void CountdownText_Loaded(object sender, RoutedEventArgs e)
        {
            Application.Current.Host.Content.Resized += new EventHandler(Content_Resized);
            UpdateDisplay();
        }

        private void LayoutRoot_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // http://www.c-sharpcorner.com/UploadFile/dpatra/1107/
            // e.Handled = true;
        }

        private void LayoutRoot_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //cMenu = new ContextMenu();

            //menuItem = new MenuItem();
            //menuItem.Header = "Go full screen";
            //cMenu.Items.Add(menuItem);
            //menuItem = new MenuItem();
            //menuItem.Header = "Reset";
            //cMenu.Items.Add(menuItem);
            //menuItem = new MenuItem();
            //menuItem.Header = "Start";
            //cMenu.Items.Add(menuItem);

            //cMenu.IsOpen = true;
            //cMenu.HorizontalOffset = e.GetPosition(LayoutRoot).X;
            //cMenu.VerticalOffset = e.GetPosition(LayoutRoot).Y;
        }

        private void ControlBlock_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            HoverFocus.Begin();
        	// TODO: Add event handler implementation here.
        }

        private void ControlBlock_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            HoverUnfocus.Begin();
        }
    }
}